# List practice

# 01. 写一个 reverse 函数
# 它接受一个 list 作为参数
# 返回这个 list 的逆序
# reverse([3, 5, 1, 6]) -> [6,1,5,3]
# 
'''
-len(arr)

arr    [3, 5, 1, 6]
result []

result.insert(arr[0], 0) # [3]
result.insert(arr[1], 0) # [5, 3]
result.insert(arr[2], 0) # [1, 5, 3]
result.insert(arr[3], 0) # [6, 1, 5, 3]

result.append(arr[-1])
result.append(arr[-2])
result.append(arr[-3])
...
result.append(arr[-len(arr)])
'''
def reverse1(arr: list) -> list:
    result = []

    for i in range(-1, -len(arr) - 1, -1):
        result.append(arr[i])

    return result

def reverse_2(arr: list) -> list:
    result = []
    for i in range(0, len(arr)):
        result.insert(arr[i], 0)
    return result

def reverse (arr: list) -> list:
    result = []
    for i in range(0, len(arr)):
        result.insert(arr[i],-0)
        return result
print (reverse([3, 5, 1, 6]))
    
'''
02. Combine
list1 = ["M", "na", "i", "Dan"]
list2 = ["y", "me", "s", "iel"]

["My", "name", "is", "Daniel"]

combine 接受两个元素个数相等的列表作为参数
把对应位置的元素合并之后，变成一个新的列表

'''

def combine(list1: list, list2: list) -> list:
    result = []
    for i in range(0, len(list1)):
        s = list1[i] + list2[i] # 合并对应位置的元素
        result.append(s)
    return result

print(combine(list1 = ["M", "na", "i", "Dan"], list2 = ["y", "me", "s", "iel"]))

'''
03. remove_empty
删除列表中的所有空字符串
remove_empty(["Mike", "", "Emma", "Kelly", "", "Brad"])
["Mike", "Emma", "Kelly", "Brad"]
'''
def remove_empty(arr: list) -> list:
    result = []
    # 不能在 for 循环里，删除列表的内容
    for s in arr:
        if s != "":
            result.append(s)

    return result

print(remove_empty(["Mike", "", "Emma", "Kelly", "", "Brad"]))